package co.yixiang.yshop.module.product.service.storeproductrelation;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.StoreProductRelationPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.StoreProductRelationRespVO;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductrelation.StoreProductRelationDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductrelation.StoreProductRelationMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_RELATION_NOT_EXISTS;

/**
 * 商品点赞和收藏 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreProductRelationServiceImpl implements StoreProductRelationService {

    @Resource
    private StoreProductRelationMapper storeProductRelationMapper;


    @Override
    public void deleteStoreProductRelation(Long id) {
        // 校验存在
        validateStoreProductRelationExists(id);
        // 删除
        storeProductRelationMapper.deleteById(id);
    }

    private void validateStoreProductRelationExists(Long id) {
        if (storeProductRelationMapper.selectById(id) == null) {
            throw exception(STORE_PRODUCT_RELATION_NOT_EXISTS);
        }
    }


    @Override
    public PageResult<StoreProductRelationRespVO> getStoreProductRelationPage(StoreProductRelationPageReqVO pageReqVO) {

        Page<StoreProductRelationDO> pageModel = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<StoreProductRelationRespVO> list = storeProductRelationMapper
                .allRelationList(pageModel, pageReqVO.getNickname(), pageReqVO.getType());
        return new PageResult<>(list, storeProductRelationMapper.allRelationListCount(pageReqVO.getNickname(), pageReqVO.getType()));
    }


}
